$(function(){
    $("#tree").tree({
        data: {
            type: "json",
            url: "../security/dept!searchByParentId.action",
            async: true,
            async_data: function(node){
                return {
                    id: $(node).attr("id") || 0
                }
            }
        },
        rules: {
            multitree: true,
            draggable: "all"
        },
        lang: {
            loading: "加载中..."
        },
        /*ui:{//在这个option中设置context来控制JSTree的右键操作，如果在context的visible函数内始终返回false则表示在任何节点的右键都无效。
         context:{visible : function (node, tree_obj) {
         return false;
         }
         }
         }*/
        ui: {
            theme_name: "classic",
            context: [ //自定义右键操作的可操作内容
            {
                id: "create",
                label: "添加", //右键弹出菜单的此项操作屏显字样
                visible: function(node, tree_obj){
                    if (node.length != 1) 
                        return false;
                    return tree_obj.check("creatable", node);
                }, //允许节点被右击时出现操作菜单
                icon: "create.png",//右键弹出菜单的此项操作图标
                action: function(node, tree_obj){
                    //进行此项操作，将有这个函数事件被响应
                    tree_obj.create(false, tree_obj.get_node(node));
					$("#parentId").val($(node).attr("id"));
                    //document.getElementById("parentId").value = $(node).attr("id");
                }
            }, "separator", {//'separator'这个是在两个操作之间画一条分隔线
                id: "rename",
                label: "重命名",
                visible: function(node, tree_obj){
                    if (node.length != 1) 
                        return false;
                    return tree_obj.check("creatable", node);
                },
                icon: "rename.png",
                action: function(node, tree_obj){
                    tree_obj.rename();
                }
            }, "separator", {
                id: "delete",
                label: "删除",
                visible: function(node, tree_obj){
                    if (node.length != 1) 
                        return false;
                    return tree_obj.check("creatable", node);
                },
                icon: "remove.png",
                action: function(node, tree_obj){
                    $.each(node, function(){
                        if (!$(node).attr("id")) {
                            tree_obj.remove(this);
                        }
                        else {
                            $.getJSON('../security/dept!delete.action', {
                                id: $(node).attr("id")
                            }, function(data){
                                $.tree_reference("tree").refresh();
                                alert(data['msg']);
                            });
                        }
                    });
                }
            }]
        },
        callback: {
            onselect: function(node){
                $.getJSON('../security/dept!searchById.action', {
                    id: $(node).attr("id")
                }, function(data){
                    if (data['id'] == 0) {
                        $("#name").val("");
                    }
                    else {
                        $("#id").val(data['id']);
                    }
                    $("#name").val(data['name']);
                    $("#manager").val(data['manager']);
                    $("#indexs").val(data['indexs']);
                    $("#description").val(data['description']);
                });
            },
            onmove: function(node, ref_node, tree_obj){
                $.getJSON('../security/dept!move.action', {
                    id: $(node).attr("id"),
                    parentId: $(ref_node).attr("id")
                }, function(data){
                    $.tree_reference("tree").refresh();
                    alert(data['msg']);
                });
            }/*,
            onchange: function(NODE, TREE_OBJ){
                if (TREE_OBJ.settings.ui.theme_name == "checkbox") {
                    var $this = $(NODE).is("li") ? $(NODE) : $(NODE).parent();
                    if ($this.children("a.unchecked").size() == 0) {
                        TREE_OBJ.container.find("a").addClass("unchecked");
                    }
                    $this.children("a").removeClass("clicked");
                    if ($this.children("a").hasClass("checked")) {
                        $this.find("li").andSelf().children("a").removeClass("checked").removeClass("undetermined").addClass("unchecked");
                        var state = 0;
                    }
                    else {
                        $this.find("li").andSelf().children("a").removeClass("unchecked").removeClass("undetermined").addClass("checked");
                        var state = 1;
                    }
                    $this.parents("li").each(function(){
                        if (state == 1) {
                            if ($(this).find("a.unchecked, a.undetermined").size() - 1 > 0) {
                                $(this).parents("li").andSelf().children("a").removeClass("unchecked").removeClass("checked").addClass("undetermined");
                                return false;
                            }
                            else 
                                $(this).children("a").removeClass("unchecked").removeClass("undetermined").addClass("checked");
                        }
                        else {
                            if ($(this).find("a.checked, a.undetermined").size() - 1 > 0) {
                                $(this).parents("li").andSelf().children("a").removeClass("unchecked").removeClass("checked").addClass("undetermined");
                                return false;
                            }
                            else 
                                $(this).children("a").removeClass("checked").removeClass("undetermined").addClass("unchecked");
                        }
                    });
                }
            }*/
        }
    });
});


$(function(){
    $('#inputForm').ajaxForm({
        success: function(){
            $.tree_reference("tree").refresh();
            alert("保存成功");
            
        }
    });
});
